const router = require('koa-router')()
const Mock = require('mockjs')
let { list } = Mock.mock({
  "list|50": [{
    "id": "@id",
    "nickname": "@cname",
    "tel": /1[3-9]\d{9}/,
    "address": "@county(true)"
  }]
})
// 增
router.post('/list', ctx => {
  const { nickname, tel, address } = ctx.request.body;
  const data = {
    nickname, tel, address, id: Mock.Random.id()
  }
  list.unshift(data);
  ctx.body = {
    code: 200,
    message: 'ok'
  }
})
// 删
router.delete('/list', ctx => {
  const { id } = ctx.request.query;
  // id = 123,345,1231,12,31231,
  // 删除， 支持多个id 删除
  list = list.filter(v => !id.includes(v.id));

  ctx.body = {
    code: 200,
    message: 'ok'
  }

})
// 改
router.post('/list/edit', ctx => {
  const { id, editData } = ctx.request.body;
  const item = list.find(v => v.id === id);

  Object.assign(item, editData)
  
  ctx.body = {
    code: 200,
    message: 'ok'
  }
})
// 查
router.get('/list', ctx => {
  ctx.body = {
    code: 200,
    data: list,
    message: 'ok'
  } 
})
module.exports = router
